<?php

/**
 * 订单主表
 * 模型对应数据库表 "orders".
 *
 * 以下是可用表orders的栏位:
 * @property string $id
 * @property string $order_number
 * @property string $site_key
 * @property string $currency_tag
 * @property string $total_price
 * @property string $comment
 * @property string $coupon
 * @property string $offset
 * @property string $order_date
 * @property string $pay_status
 * @property string $status
 * @property string $uid
 * @property string $pay_type
 */
class Orders extends CActiveRecord {

    public $totel,$pay_oreder_number;

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return Orders the static model class
     * @author nirsh
     * @example
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'orders';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('pay_type', 'required'),
            array('order_number', 'length', 'max' => 20),
            array('site_key', 'length', 'max' => 255),
            array('currency_tag', 'length', 'max' => 3),
            array('total_price, offset', 'length', 'max' => 10),
            array('coupon, pay_type', 'length', 'max' => 50),
            array('order_date, uid', 'length', 'max' => 11),
            array('pay_status, status', 'length', 'max' => 2),
            array('comment', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, order_number, site_key, currency_tag, total_price, comment, coupon, offset, order_date, pay_status, status, uid, pay_type', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'order_number' => '订单编号',
            'site_key' => '分站',
            'currency_tag' => '货币',
            'total_price' => '订单总额',
            'comment' => '备注',
            'coupon' => '打折码',
            'offset' => '优惠金额',
            'order_date' => '下间时间',
            'pay_status' => '支付状态',
            'status' => '订单状态',
            'uid' => 'Uid',
            'pay_type' => '支付类型',
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('order_number', $this->order_number);
        $criteria->compare('site_key', $this->site_key, true);
        $criteria->compare('currency_tag', $this->currency_tag);
        $criteria->compare('total_price', $this->total_price, true);
        $criteria->compare('comment', $this->comment, true);
        $criteria->compare('coupon', $this->coupon);
        $criteria->compare('order_date', $this->order_date, true);
        $criteria->compare('pay_status', $this->pay_status);
        $criteria->compare('status', $this->status);
        $criteria->compare('uid', $this->uid);
        $criteria->compare('pay_type', $this->pay_type);
        if($this->pay_oreder_number){
            foreach ($this->pay_oreder_number as $payArray) {
                if($payArray->order_number){
                     $numberArray[] = $payArray->order_number;
                }
            }
            if(!empty($numberArray)){
                 $criteria->addInCondition('order_number', $numberArray);
            }
        }
        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'sort'=>array(
                'defaultOrder' => 'order_date DESC',
            ),
        ));
    }

    /**
      +-----------------------------------------------------
     * 说明生成订单报表数据 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getOrderSiteSeries() {
        $sArray = array();
        $cdbc = new CDbCriteria;
        $cdbc->select = "COUNT(*) AS totel,site_key";
        $cdbc->group = "site_key";
        $data = $this->findAll($cdbc);
        foreach ($data as $v) {
            $sArray[] = array('name' => $v->site_key, 'y' => (int) $v->totel);
        }
        $pieArray = array(
            'type' => 'pie',
            'name' => '网站订单数量报表',
            'data' => $sArray
        );
        return $pieArray;
    }

    /**
      +-----------------------------------------------------
     * 说明 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getMoneySiteSeries() {
        $sArray = array();
        $cdbc = new CDbCriteria;
        $cdbc->select = "count(*) AS totel,status";
        $cdbc->group = "status";
        $data = $this->findAll($cdbc);
        foreach ($data as $v) {
            $sArray[] = array('name' => $v->status, 'y' => (int) $v->totel);
        }
        $pieArray = array(
            'type' => 'pie',
            'name' => '网站订单状态报表',
            'data' => $sArray
        );
        return $pieArray;
    }

    /**
      +-----------------------------------------------------
     * 说明  获取价格类型
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getPriceString($price,$c_tag) {
        $c_tag_string = WebCurrency::model()->find("currency_code='".$c_tag."'")->currency_tag;
        $cTag = $c_tag_string?$c_tag_string:"$";
        return $cTag.$price;
    }

}